package com.zjdiepu.www.manage.api.dh;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.status.OverduePhaseStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.base.enums.type.TypeData;
import com.zjdiepu.www.base.enums.type.TypeTrailer;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.common.util.UUIDFactory;
import com.zjdiepu.www.ibiz.dh.IDhOverdueListService;
import com.zjdiepu.www.ibiz.dh.IDhOverdueTrailerRecordService;
import com.zjdiepu.www.ibiz.dh.IDhOverdueTrailerService;
import com.zjdiepu.www.ibiz.system.IBaseBusiUnitService;
import com.zjdiepu.www.ibiz.system.IBaseCustomerService;
import com.zjdiepu.www.ibiz.system.IBaseSalersmanService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessExtrasService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.model.dh.dto.DhOverdueList;
import com.zjdiepu.www.model.dh.dto.DhOverdueTrailer;
import com.zjdiepu.www.model.dh.dto.DhOverdueTrailerRecord;
import com.zjdiepu.www.model.dh.ext.DhOverdueTrailerRecordExt;
import com.zjdiepu.www.model.system.dto.BaseBusiUnit;
import com.zjdiepu.www.model.system.dto.BaseSalersman;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.dto.YwBusinessExtras;

/**
 * Controller层<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年06月08日 15时30分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "DhOverdueTrailerRecordController", produces="application/json;charset=UTF-8")
@Api(tags = {""}, produces="application/json;charset=UTF-8")
public class DhOverdueTrailerRecordController extends BaseController {
    @Resource
    private IDhOverdueTrailerRecordService dhOverdueTrailerRecordService;
    
    @Resource
    private IDhOverdueTrailerService dhOverdueTrailerService ;
    @Resource
    private IYwBusinessService ywBusinessService;
    @Resource
    private IBaseCustomerService baseCustomerService;
    @Resource
    private IBaseSalersmanService baseSalersmanService;
    @Resource
    private IBaseBusiUnitService baseBusiUnitService;
    @Resource
    private IYwBusinessExtrasService ywBusinessExtrasService;
    @Resource
    private IDhOverdueListService dhOverdueListService;

    /**
     * @author zhangzhengjian <br/>
     * @Title: save  <br/>
     * @Description: 新增 <br/>
     * @param dhOverdueTrailerRecord
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "新增", response = Boolean.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public ApiResponse save(@Validated(Add.class) @ModelAttribute DhOverdueTrailerRecord dhOverdueTrailerRecord) {
    	String overdueTrailerId=dhOverdueTrailerRecord.getId();
    	AbstractSysUser currentUser = getCurrentUser();
    	dhOverdueTrailerRecord.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dhOverdueTrailerRecord.setModifierName(currentUser.getName());
        try {
			dhOverdueTrailerRecordService.saveUpdateAndDelete(dhOverdueTrailerRecord, OperateType.add);
		} catch (Exception e) {
			e.printStackTrace();
		}
        DhOverdueTrailer dhOverdueTrailer= new DhOverdueTrailer();
        dhOverdueTrailer.setId(overdueTrailerId);
        dhOverdueTrailer.setStatusTrailer(dhOverdueTrailerRecord.getStatusTrailer());
        dhOverdueTrailerService.saveUpdateAndDelete(dhOverdueTrailer, OperateType.edit);
        return ApiResponse.success(true);
    }
    /**
     * 拖车记录保存
     * @param dhOverdueTrailerRecord
     * @return
     */
    @ApiOperation(value = "拖车记录保存", response = Boolean.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public ApiResponse insert(@Validated(Add.class) @ModelAttribute DhOverdueTrailerRecord dhOverdueTrailerRecord) {
    	AbstractSysUser currentUser = getCurrentUser();
    	Integer current = (int)(System.currentTimeMillis() / 1000);
    	String uuid=UUIDFactory.getUUID();
    	DhOverdueTrailer dhOverdueTrailer= new DhOverdueTrailer();
    	dhOverdueTrailer.setCusName(dhOverdueTrailerRecord.getCusName());
    	dhOverdueTrailer.setBusiId(dhOverdueTrailerRecord.getBusiId());
    	dhOverdueTrailer.setCusId(dhOverdueTrailerRecord.getCusId());
    	dhOverdueTrailer.setTypeTrailer(TypeTrailer.SHANG_BAO.getCode());
    	dhOverdueTrailer.setCreated(current);
    	dhOverdueTrailer.setCreator(currentUser.getId());
    	dhOverdueTrailer.setCreatorName(currentUser.getName());
    	dhOverdueTrailer.setModified(current);
    	dhOverdueTrailer.setModifier(currentUser.getId());
    	dhOverdueTrailer.setModifierName(currentUser.getName());
    	dhOverdueTrailer.setDpStatus(DpStatus.NORMAL.getCode());
    	dhOverdueTrailer.setTypeData(TypeData.SYSTEM.getCode());
    	dhOverdueTrailer.setId(uuid);
    	dhOverdueTrailer.setStatusTrailer(dhOverdueTrailerRecord.getStatusTrailer());
    	
    	YwBusiness ywBusiness = ywBusinessService.selectById(dhOverdueTrailerRecord.getBusiId());
    	if(ywBusiness!=null){
    		BaseSalersman baseSalersman=baseSalersmanService.selectById(ywBusiness.getSalesmanId());
    		if(baseSalersman!=null){
    			BaseBusiUnit baseBusiUnit=baseBusiUnitService.selectById(baseSalersman.getUnitId());
    			if(baseBusiUnit!=null){
    				dhOverdueTrailer.setSalesmanId(ywBusiness.getSalesmanId());
    				dhOverdueTrailer.setSalesmanCode(ywBusiness.getSalesmanCode());
    				dhOverdueTrailer.setSalesmanName(baseSalersman.getName());
    				dhOverdueTrailer.setCustomerNo(ywBusiness.getCustomerNo());
    				dhOverdueTrailer.setUnitId(baseBusiUnit.getId());
    				dhOverdueTrailer.setUnitCode(baseBusiUnit.getOrgCode());
    				dhOverdueTrailer.setUnitName(baseBusiUnit.getOrgName());
    			}
    		}
    		
    	}
    	YwBusiness ywbusiness=ywBusinessService.selectById(dhOverdueTrailerRecord.getBusiId());
    	dhOverdueTrailer.setLoanAmount(ywbusiness.getSignLoanAmount());
    	dhOverdueTrailerService.saveUpdateAndDelete(dhOverdueTrailer, OperateType.add);
    	
    	dhOverdueTrailerRecord.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dhOverdueTrailerRecord.setModifierName(currentUser.getName());
    	dhOverdueTrailerRecord.setTrailerId(uuid);
        try {
			dhOverdueTrailerRecordService.saveUpdateAndDelete(dhOverdueTrailerRecord, OperateType.add);
		} catch (Exception e) {
			e.printStackTrace();
		}
        //更新逾期信息表逾期阶段
        DhOverdueList dhOverdueList= new DhOverdueList();
        dhOverdueList.setBusiId(dhOverdueTrailerRecord.getBusiId());
        DhOverdueList dhOverdueList1=dhOverdueListService.selectRefined(dhOverdueList);
        dhOverdueList1.setPhaseOverdue(OverduePhaseStatus.SHANG_MEN_CUI_SHOU.getCode());
        dhOverdueListService.saveUpdateAndDelete(dhOverdueList1, OperateType.edit);
        //更新到业务信息-附加信息表电催人id
        YwBusinessExtras ywBusinessExtras= new YwBusinessExtras();
        ywBusinessExtras.setBusiId(dhOverdueTrailerRecord.getBusiId());
        ywBusinessExtras.setTypeExtras("overdue");
        ywBusinessExtras.setCodeExtras("phase_overdue");
        YwBusinessExtras ywBusinessExtras1=ywBusinessExtrasService.selectRefined(ywBusinessExtras);
        if(ywBusinessExtras1!=null){
        	ywBusinessExtras1.setValueExtras(OverduePhaseStatus.SHANG_MEN_CUI_SHOU.getCode());
	        ywBusinessExtrasService.saveUpdateAndDelete(ywBusinessExtras1, OperateType.edit);
        }
        return ApiResponse.success(true);
    }

    /**
     * @author zhangzhengjian <br/>
     * @Title: remove  <br/>
     * @Description: 删除<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "删除", response = Boolean.class, httpMethod="POST", tags={"删除"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public ApiResponse remove(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dhOverdueTrailerRecordService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.DELETED.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author zhangzhengjian <br/>
     * @Title: disable  <br/>
     * @Description: 禁用<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "禁用", response = Boolean.class, httpMethod="POST", tags={"禁用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "disable", method = RequestMethod.POST)
    public ApiResponse disable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dhOverdueTrailerRecordService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.INVALID.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author zhangzhengjian <br/>
     * @Title: enable <br/>
     * @Description: 启用<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "启用", response = Boolean.class, httpMethod="POST", tags={"启用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "enable", method = RequestMethod.POST)
    public ApiResponse enable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dhOverdueTrailerRecordService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.NORMAL.getCode(), ids);
        return ApiResponse.success(true);
    }

    /**
     * @author zhangzhengjian <br/>
     * @Title: modify  <br/>
     * @Description: 修改 <br/>
     * @param dhOverdueTrailerRecord
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改", response = Boolean.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public ApiResponse modify(@Validated(Update.class) @ModelAttribute DhOverdueTrailerRecord dhOverdueTrailerRecord) {
    	AbstractSysUser currentUser = getCurrentUser();
    	dhOverdueTrailerRecord.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dhOverdueTrailerRecord.setModifierName(currentUser.getName());
    	try {
			dhOverdueTrailerRecordService.saveUpdateAndDelete(dhOverdueTrailerRecord, OperateType.edit);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return ApiResponse.success(true);
    }

    /**
     * @author zhangzhengjian <br/>
     * @Title: getById  <br/>
     * @Description: 主键查询<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "主键查询", response = DhOverdueTrailerRecord.class, httpMethod="POST", tags={"主键查询"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    public ApiResponse getById(String id){
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
       DhOverdueTrailerRecord dhOverdueTrailerRecord = dhOverdueTrailerRecordService.selectById(id);
       if(null == dhOverdueTrailerRecord) {
    	   return ApiResponse.fail(ResultFlagType.MISSING);
       }
       return ApiResponse.success(dhOverdueTrailerRecord);
    }
    
    /**
     * @author zhangzhengjian <br/>
     * @Title: list  <br/>
     * @Description: 条件查询 <br/>
     * @param dhOverdueTrailerRecordExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "条件查询",response = DhOverdueTrailerRecord.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ApiResponse list(@ModelAttribute DhOverdueTrailerRecordExt dhOverdueTrailerRecordExt){
        dhOverdueTrailerRecordExt.notDeleted();
        PageVo page = getPage(dhOverdueTrailerRecordExt);
        int records = dhOverdueTrailerRecordService.countByExt(dhOverdueTrailerRecordExt);
        page.setRecords(records);
        if (records > 0) {
            List<DhOverdueTrailerRecord> dpBaseDataDictionaries = dhOverdueTrailerRecordService.selectByExt(dhOverdueTrailerRecordExt);
            page.setRows(dpBaseDataDictionaries);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }

}
